package com.example.zhunongzonghe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhunongzonghe.beans.ZnMessageBean;
import com.example.zhunongzonghe.common.Result;
import com.example.zhunongzonghe.entity.ZnMessage;
import com.example.zhunongzonghe.entity.ZnUsers;
import com.example.zhunongzonghe.service.IZnUsersService;
import com.example.zhunongzonghe.service.ZnMessageService;
import com.example.zhunongzonghe.utils.BeanCopyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;

;

@RestController
@RequestMapping("/znMessage")
public class znMessageController {

    @Autowired
    private ZnMessageService znMessageService;
    @Autowired
    private IZnUsersService iZnUsersService;

    /**
     * 显示全部资讯
     * @param currentPage
     * @param pageNum
     * @return
     */
    @GetMapping("/SearchZnMessageAll")
    public Result SearchPostsAll(@RequestParam("currentPage") Integer currentPage,
                                 @RequestParam("pageSize") Integer pageNum,
                                 @RequestParam("input") Object searchInfo){
        if (currentPage==null && pageNum==null) return new Result(false,"数据异常",400,null);
        Page<ZnMessageBean> znMessageBeanPage = znMessageService.searchMessageAll(currentPage,pageNum,searchInfo);
        if (currentPage > znMessageBeanPage.getPages()){
            int lastPage = (int) znMessageBeanPage.getPages();
            znMessageBeanPage=znMessageService.searchMessageAll(lastPage,pageNum,searchInfo);
        }
        return new Result<>(true,"查询成功",200,znMessageBeanPage);
    }


    /**
     * 后台管理员编辑资讯
     * @param znMessage
     * @return
     */
    @PutMapping("/editZnMessage")
    public Result editZnMessage(@RequestBody ZnMessage znMessage,Principal principal){
        if (znMessage==null) return new Result(false,"数据异常",400,null);
        System.out.println("11111111"+znMessage);
        ZnUsers znUsers = iZnUsersService.getUserName(principal.getName());
        znMessage.setUpdateTime(LocalDateTime.now());
        znMessage.setUpdateBy(znUsers.getId());
        znMessageService.updateById(znMessage);
        return new Result<>(true,"更新成功",200);
    }

    /**
     * 后台管理员删除资讯
     * @param messId
     * @return
     */
    @DeleteMapping("/deleteZnMessage/{messId}")
    public Result delete(@PathVariable Integer messId){
        if (messId==null) return new Result(false,"数据异常",400,null);
        System.out.println("1111111"+messId);
        znMessageService.removeById(messId);
        return new Result<>(true,"删除成功",200);
    }
    /**
     * 后台新增资讯
     */
    @PostMapping("/addZnMessage")
    public Result addPost(@RequestBody ZnMessage znMessage, Principal principal){
        if (znMessage==null) return new Result(false,"数据异常",400,null);
        ZnUsers znUsers = iZnUsersService.getUserName(principal.getName());
        znMessage.setCreateBy(znUsers.getId());
        znMessage.setCreateTime(LocalDateTime.now());
        znMessageService.save(znMessage);
        return new Result(true,"添加成功",200);
    }

    /**
     * 交流中心获取助农资讯
     * @return
     */
    @GetMapping("/getZnMessageAll")
    public Result getZnMessageAll(){
        List<ZnMessage> messageList = znMessageService.list();
        List<ZnMessageBean> beanList = BeanCopyUtils.copyBeanList(messageList,ZnMessageBean.class);
        return new Result(true,"成功",200,beanList);
    }

    /**
     * 前台跳转资讯详情页
     * @param messId
     * @return
     */
    @GetMapping("/getMessageDetail/{messId}")
    public Result getDetailPost(@PathVariable Integer messId){
        if (messId == null){
            return new Result(false,"数据异常，请联系管理员",400);
        }
        ZnMessage znMessage = znMessageService.getById(messId);
        ZnUsers userNick = iZnUsersService.getById(znMessage.getCreateBy());
        ZnMessageBean znMessageBean = new ZnMessageBean();
        znMessageBean.setNickName(userNick.getNickname());
        BeanUtils.copyProperties(znMessage,znMessageBean);
        return new Result(true,"成功",200,znMessageBean);
    }

}
